﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bussiness.Interface;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;
using Bussiness.BaseLogic;

namespace Bussiness.ModelQuery
{
    public class OrderManager
    {
        public OrderManager()
        {

        }
        private static OrderManager instance;
        public static OrderManager Instance
        {
            get 
            {
                if (instance == null)
                {
                    instance = new OrderManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, Order> orderCaches = new Dictionary<string, Order>();
        Dictionary<string, Order> orderPayedCaches = new Dictionary<string, Order>();
        Dictionary<string, Order> orderNotPayedCaches = new Dictionary<string, Order>();
        /// <summary>
        /// 加载数据
        /// </summary>
        public void LoadData()
        {
            orderCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Order>();
            crt.AddOrder(new NHibernate.Criterion.Order("CreatTime", true));//按订单创建升序排列
            IList<Order> orderList = null;
            orderList = crt.List<Order>();
            if (orderList != null)
            {
                foreach (Order order in orderList)
                {
                    orderCaches.Add(order.ID, order);
                    if (order.IsPayed)
                    {
                        orderPayedCaches.Add(order.ID, order);
                    }
                    else
                    {
                        orderNotPayedCaches.Add(order.ID, order);
                    }
                }
            }
        }
        /// <summary>
        /// 获取所有的订单信息
        /// </summary>
        /// <returns></returns>
        public Order[] GetOrders()
        {
            return orderCaches.Values.ToArray();
        }
        /// <summary>
        /// 根据订单Id获取订单
        /// </summary>
        /// <param name="sId">订单Id</param>
        /// <returns>订单信息</returns>
        public Order GetOrderById(string sId)
        {
            if (orderCaches.ContainsKey(sId))
            {
                return orderCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取用户的所有订单信息
        /// </summary>
        /// <param name="sUserId">用户Id</param>
        /// <returns>订单信息</returns>
        public Order[] GetOrdersByUserId(string sUserId)
        {
            List<Order> orderList = new List<Order>();
            QueryOrderByUserId(sUserId, orderCaches.Values.ToArray(), out orderList);
            return orderList.ToArray();
        }
        /// <summary>
        /// 获取所有付款(未付款)的订单
        /// </summary>
        /// <param name="isPayed">是否付款</param>
        /// <returns>订单信息</returns>
        public Order[] GetOrdersIsPayed(bool sIsPayed)
        {
            if (sIsPayed)
            {
                return orderPayedCaches.Values.ToArray();
            }
            else
            {
                return orderNotPayedCaches.Values.ToArray();
            }
        }
        /// <summary>
        /// 获取用户所有付款(未付款)的订单
        /// </summary>
        /// <param name="sUserId">用户Id</param>
        /// <param name="sIsPayed">是否付款</param>
        /// <returns>订单信息</returns>
        public Order[] GetOrdersIsPayedByUserId(string sUserId, bool sIsPayed)
        {
            List<Order> orderList = new List<Order>();
            if (sIsPayed)
            {
                QueryOrderByUserId(sUserId, orderPayedCaches.Values.ToArray(), out orderList);
            }
            else
            {
                QueryOrderByUserId(sUserId, orderNotPayedCaches.Values.ToArray(), out orderList);     
            }
            return orderList.ToArray();
        }
        /// <summary>
        /// 查询用户订单
        /// </summary>
        /// <param name="sUserId">用户Id</param>
        /// <param name="sOrders">订单集合</param>
        /// <param name="sList">用户订单集合</param>
        private void QueryOrderByUserId(string sUserId, Order[] sOrders, out List<Order> sList)
        {
            sList = new List<Order>();
            foreach (Order order in sOrders)
            {
                if (order.UserID == sUserId)
                {
                    sList.Add(order);
                }
            } 
        }
        /// <summary>
        /// 获取给定日期创建的所有订单
        /// </summary>
        /// <param name="sTime"></param>
        /// <returns></returns>
        public Order[] GetOrdersByCreateTime(DateTime sTime)
        {
            List<Order> orderList = new List<Order>();
            foreach (Order order in orderCaches.Values)
            {
                bool isSameDay = DateTimeHelper.IsTheSameDay(order.CreateTime, sTime);
                if (isSameDay)
                {
                    orderList.Add(order);
                }
            }
            return orderList.ToArray();
        }
        /// <summary>
        /// 获取给定日期完成的所有订单
        /// </summary>
        /// <param name="sTime"></param>
        /// <returns></returns>
        public Order[] GetOrdersByFinishTime(DateTime sTime)
        {
            List<Order> orderList = new List<Order>();
            foreach (Order order in orderCaches.Values)
            {
                bool isSameDay = DateTimeHelper.IsTheSameDay(order.FinishTime, sTime);
                if (isSameDay)
                {
                    orderList.Add(order);
                }
            }
            return orderList.ToArray();
        }
        /// <summary>
        /// 获取给定日期创建的已付款/未付款订单
        /// </summary>
        /// <param name="sTime">给定日期</param>
        /// <param name="isPayed">是否付款</param>
        /// <returns></returns>
        public Order[] GetOrdersIsPayedByCreateTime(DateTime sTime, bool isPayed)
        {
            List<Order> orderList = new List<Order>();
            if (isPayed)
            {
                foreach (Order order in orderPayedCaches.Values)
                {
                    bool isSameDay = DateTimeHelper.IsTheSameDay(order.CreateTime, sTime);
                    if (isSameDay)
                    {
                        orderList.Add(order);
                    }
                }
            }
            else
            {
                foreach (Order order in orderNotPayedCaches.Values)
                {
                    bool isSameDay = DateTimeHelper.IsTheSameDay(order.CreateTime, sTime);
                    if (isSameDay)
                    {
                        orderList.Add(order);
                    }
                }
            }
            return orderList.ToArray();
        }
        /// <summary>
        /// 获取用户在给定日期创建的所有订单
        /// </summary>
        /// <param name="sUserId">用户ID</param>
        /// <param name="sTime">给定日期</param>
        /// <returns></returns>
        public Order[] GetUserOrdersByCreateTime(string sUserId, DateTime sTime)
        {
            List<Order> orderList = new List<Order>();
            foreach (Order order in orderCaches.Values)
            {
                if (order.UserID == sUserId)
                {
                    bool isSameDay = DateTimeHelper.IsTheSameDay(order.CreateTime, sTime);
                    if (isSameDay)
                    {
                        orderList.Add(order);
                    }
                }
            }
            return orderList.ToArray();
        }
        /// <summary>
        /// 获取用户在给定日期已经付款/未付款的订单
        /// </summary>
        /// <param name="sUserId">用户ID</param>
        /// <param name="sTime">给定日期</param>
        /// <param name="isPayed">是否付款</param>
        /// <returns></returns>
        public Order[] GetUserIsPayedOrdersByCreateTime(string sUserId, DateTime sTime, bool isPayed)
        {
            List<Order> orderList = new List<Order>();
            if (isPayed)
            {
                foreach (Order order in orderPayedCaches.Values)
                {
                    if (order.UserID == sUserId)
                    {
                        bool isSameDay = DateTimeHelper.IsTheSameDay(order.CreateTime, sTime);
                        if (isSameDay)
                        {
                            orderList.Add(order);
                        }
                    }
                }
            }
            else
            {
                foreach (Order order in orderNotPayedCaches.Values)
                {
                    if (order.UserID == sUserId)
                    {
                        bool isSameDay = DateTimeHelper.IsTheSameDay(order.CreateTime, sTime);
                        if (isSameDay)
                        {
                            orderList.Add(order);
                        }
                    }
                }
            }
            return orderList.ToArray();
        }
        /// <summary>
        /// 查询用户订单
        /// </summary>
        /// <param name="sTime">日期</param>
        /// <param name="sOrders">订单集合</param>
        /// <param name="sList">用户订单集合</param>
        private void QueryOrderByCreateTime(DateTime sTime, Order[] sOrders, out List<Order> sList)
        {
            sList = new List<Order>();
            foreach (Order order in sOrders)
            {
                if (order.CreateTime.Date == sTime.Date)
                {
                    sList.Add(order);
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteOrderByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            Order order = GetOrderById(sId);
            if (order != null)
            {
                session.Delete(order);
                orderCaches.Remove(sId);
                if (order.IsPayed)
                {
                    orderPayedCaches.Remove(sId);
                }
                else
                {
                    orderNotPayedCaches.Remove(sId);
                }
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="order"></param>
        public void InsertOrder(Order order)
        {
            NHHelper.SaveOjb(order);
            orderCaches.Add(order.ID, order);
            if (order.IsPayed)
            {
                orderPayedCaches.Add(order.ID, order);
            }
            else
            {
                orderNotPayedCaches.Add(order.ID, order);
            }
        }
    }
}
